import 'dart:convert' show json;
import 'package:flutter/cupertino.dart';
import 'base_chart_formatter.dart';
import 'base_model_asT.dart';
import 'package:ribo_base_core/base_styles/base_chart_style.dart';

class BaseChartChinaMapModel {
  /// https://echarts.apache.org/zh/option.html#tooltip.formatter
  BaseChartFormatter tooltipFormatter;

  /// https://echarts.apache.org/zh/option.html#series-pie.label.formatter
  BaseChartFormatter labelFormatter;

  /// 图表样式
  BaseChartStyle style;

  /// 地图名称
  dynamic name;

  /// 城市代码
  String code;

  /// https://echarts.apache.org/zh/option.html#visualMap-continuous.text
  List<String> visualMapTexts;

  /// 左下角分层样式
  List<BaseChartPiecesData> pieces;

  /// 图表数据
  List<BaseChartChinaMapData> data;

  /// 是否开启缩放和平移
  bool roam;

  /// 是否显示放大图标
  bool showFullScreen;

  BaseChartChinaMapModel({
    this.tooltipFormatter,
    this.labelFormatter,
    this.style,
    this.name,
    this.code,
    this.pieces,
    this.roam = false,
    this.showFullScreen = true,
    this.visualMapTexts = const ['',''],
    @required this.data,
  });

  factory BaseChartChinaMapModel.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<BaseChartChinaMapData> data =
    jsonRes['data'] is List ? <BaseChartChinaMapData>[] : null;
    if (data != null) {
      for (final dynamic item in jsonRes['data']) {
        if (item != null) {
          data.add(
              BaseChartChinaMapData.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }
    return BaseChartChinaMapModel(
      tooltipFormatter: asT<BaseChartFormatter>(jsonRes['tooltipFormatter']),
      labelFormatter: asT<BaseChartFormatter>(jsonRes['labelFormatter']),
      style: asT<BaseChartStyle>(jsonRes['style']),
      name: asT<String>(jsonRes['name']),
      code: asT<String>(jsonRes['code']),
      roam: asT<bool>(jsonRes['roam']),
      visualMapTexts: asT<List<String>>(jsonRes['code']),
      pieces: asT<List<BaseChartPiecesData>>(jsonRes['pieces']),
      data: data,
    );
  }

  Map<String, dynamic> toJson() =>
      <String, dynamic>{
        'tooltipFormatter': tooltipFormatter,
        'labelFormatter': labelFormatter,
        'style': style,
        'code': code,
        'roam': roam,
        'name': name,
        'pieces': pieces,
        'visualMapTexts': visualMapTexts,
        'data': data,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class BaseChartPiecesData {
  BaseChartPiecesData({
    this.gt,
    this.gte,
    this.lt,
    this.lte,
    this.value,
    this.label,
    this.color,
  });

  factory BaseChartPiecesData.fromJson(Map<String, dynamic> jsonRes) =>
      jsonRes == null
          ? null
          : BaseChartPiecesData(
        gt: asT<double>(jsonRes['gt']),
        gte: asT<double>(jsonRes['gte']),
        lt: asT<double>(jsonRes['lt']),
        lte: asT<double>(jsonRes['lte']),
        value: asT<double>(jsonRes['value']),
        label: asT<String>(jsonRes['label']),
        color: asT<String>(jsonRes['color']),
      );

  double gt;
  double gte;
  double lt;
  double lte;
  double value;
  String label;
  String color;

  Map<String, dynamic> toJson() {
    var json = {
      'gt': gt,
      'gte': gte,
      'lt': lt,
      'lte': lte,
      'value': value,
      'label': label,
      'color': color
    };
    json.removeWhere((key, value) => value == null);
    return json;
  }

  @override
  String toString() {
    return json.encode(this);
  }
}

/// https://echarts.apache.org/examples/zh/editor.html?c=doc-example/map-example
class BaseChartChinaMapData {
  BaseChartChinaMapData({
    this.name,
    this.label,
    this.value,
    this.tooltip,
  });

  factory BaseChartChinaMapData.fromJson(
      Map<String, dynamic> jsonRes) =>
      jsonRes == null
          ? null
          : BaseChartChinaMapData(
        name: asT<String>(jsonRes['name']),
        label: asT<String>(jsonRes['label']),
        value: asT<double>(jsonRes['value']),
        tooltip: asT<dynamic>(jsonRes['tooltip']),
      );

  /// 数据名称
  dynamic name;

  /// 展示名称
  String label;

  /// 数据值
  dynamic value;

  /// 提示文本信息
  dynamic tooltip;

  Map<String, dynamic> toJson() =>
      <String, dynamic>{
        'name': name,
        'label': label,
        'value': value,
        'tooltip': tooltip,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
